<template>
  <div id="main">
    <div id="" class="card">
      <div style="width: 1000px;height: auto; margin: auto;padding: 5px">
        <!--操作按钮-->
        <el-form :inline="true" class="demo-form-inline center" >
          <el-row :gutter="18">
            <el-col :span="6"><el-button type="success" icon="el-icon-success" plain @click="addStock">确认收货入库</el-button></el-col>
            <el-col :span="3"><el-button icon="el-icon-refresh" plain onclick="history.go(-1)">返回</el-button></el-col>
          </el-row>
        </el-form>
        <el-form :inline="true" class="demo-form-inline">
          <el-row>
            <el-col :span="16">
              <el-form-item label="订单编号:">
                <input v-model="PurchaseOrders.purchaseOrderNumber" style="border: none" readonly>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="总计:">
                <input v-model="PurchaseOrders.purchaseOrderAmount"  style="border: none">
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <el-form-item label="登记人:">
                <input v-model="PurchaseOrders.register" style="border: none" readonly>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="登记时间:">
                <el-input v-model="PurchaseOrders.purchaseOrderTime" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!--数据表格-->
          <el-table
              :data="orders">
            <el-table-column
                prop="imgUrl"
                label="图片"
                width="120">
            </el-table-column>
            <el-table-column
                prop="productName"
                label="商品名称"
                width="120">
            </el-table-column>
            <el-table-column
                prop="productPrice"
                label="单价(元)"
                width="120">
            </el-table-column>
            <el-table-column
                prop="count"
                label="数量(件)"
                width="120">
            </el-table-column>
            <el-table-column
                prop="total"
                label="小计(元)"
                width="120">
              <template slot-scope="scope">
                <span>{{scope.row.total}}元</span>
              </template>
            </el-table-column>
            <el-table-column
                prop="storageTotal"
                label="确认入库件数"
                width="120">
              <template slot-scope="scope">
            <span>
              <el-input v-model="storageTotal[scope.$index]" @blur="addDetail"></el-input>
            </span>
              </template>
            </el-table-column>
            <el-table-column
                prop="stockId"
                label="仓库(最大存储量/当前存储)"
                width="230">
              <template slot-scope="scope">
              <el-select v-model="inStock.stockId">
                <el-option
                    v-for="type in stock"
                    :label="type.stockName+'('+type.maxCapacityAmount+'/'+type.stockAmount+')'"
                    :value="type.id"
                    :key="type.id">
                </el-option>
              </el-select>
              </template>
            </el-table-column>
          </el-table>
          <div class="card">
            <el-row>
              <el-col :span="16">
                <el-form-item label="入库人:">
                  <el-input v-model="inStock.storage" ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="申请入库时间:">
                  <el-input v-model="inStock.registerTime"  style="border: none" disabled></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "PurInStock",
  data() {
    return {
      //仓库数据
      stock:[],
      //入库数据
      inStock:{
        stockId:'',
        storage:'',
        registerTime:'',
        reason:'R001-1',
        total:'',
        amount:'',
        checkTag:'S001-1',
        register:''
      },
      //采购单
      PurchaseOrders: {},
      //采购订单详情
      orders: [],
      //入库明细
      inStockDetail:[],
      //应入库数数组
      storageTotal:[]
    }
  },
  created() {
    this.inStock.registerTime = this.nowDate()
    this.queryStock()
    this.selPurInStock()
  },
  methods: {
    //获取当前时间
    nowDate() {
      let getTime = new Date().getTime();
      let time = new Date(getTime);
      let year = time.getFullYear();//年
      let month = (time.getMonth() + 1).toString().padStart(2, '0');
      let date = time.getDate().toString().padStart(2, '0');
      let hour = time.getHours().toString().padStart(2, '0');
      let minute = time.getMinutes().toString().padStart(2, '0');
      let second = time.getSeconds().toString().padStart(2, '0');
      return (
          year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second
      )
    },
    addDetail(){
      for(let i=0;i<this.orders.length;i++){
        console.log(this.inStock.stockId)
        let detail={}
        detail.gatherId=this.inStock.stockId
        detail.productId=this.orders[i].productId
        detail.productName=this.orders[i].productName
        detail.amount=this.orders[i].count
        detail.costPrice=this.orders[i].productPrice
        detail.subtotal=this.orders[i].total
        detail.storageTotal=this.storageTotal[i]
        detail.gatherTag="G001-1"
        detail.supplierId=this.orders[i].supplierId
        this.inStockDetail.push(detail)
      }
    },
    //初始化仓库列表
    async queryStock(){
      const {data: res} = await this.$http.post
      (`/stock/queryAll`)
      this.stock=res
    },
    //查询要入库的采购单
    async selPurInStock(){
      const id=sessionStorage.getItem("inStockId")
      const {data :res}=await this.$http.post
      (`purchaseOrder/getOne/${id}`)
      this.PurchaseOrders=res

      const {data :orders}=await this.$http.post
      (`purchaseOrderDetails/getByParentId/${id}`)
      this.orders=orders
    },
    //入库收货
    async addStock(){
      const id=sessionStorage.getItem("inStockId")
      this.inStock.total=this.PurchaseOrders.purchaseOrderNum
      this.inStock.amount=this.PurchaseOrders.purchaseOrderAmount
      console.log(this.inStock)
      this.inStock.list=this.inStockDetail
      const {data :res}=await this.$http.post
      (`storage/addStorage/${id}`,this.inStock)
      if(res=="ok"){
        this.$message({
          message: '操作成功！需要审核',
          type: 'success'
        })
        this.$router.push("/PurQuery")
      }
    }
  }
}
</script>

<style scoped>
.card {
  box-sizing: border-box;
  padding: 20px;
  overflow-x: hidden;
  background-color: #ffffff;
  border: 1px solid #e4e7ed;
  border-radius: 6px;
  box-shadow: 0 0 12px #0000000d
}
.page{
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>